package com.demo2;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;


public class Main {
    public static void main1(String[] args) {
        System.out.println("Hello, World!");
    }

    public static void main2(String[] args) throws IOException, InterruptedException {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://www.baidu.com/"))
                .build();
//        client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
//            .thenApply(HttpResponse::body)
//      .thenAccept(System.out::println)
//      .join();
        String body = client.send(request, HttpResponse.BodyHandlers.ofString()).body();
        System.out.println(body);
    }

    public static void main3(String[] args) throws JsonProcessingException {
        String json = "{\"name\": \"mkyong\", \"age\": 20}";

        // Jackson main object
        ObjectMapper mapper = new ObjectMapper();
        // read the json strings and convert it into JsonNode
        JsonNode node = mapper.readTree(json);

        // display the JsonNode
        System.out.println("Name: " + node.get("name").asText());
        System.out.println("Age: " + node.get("age").asInt());

    }

    public static void main4(String[] args) throws IOException, InterruptedException {
        // http://dmwapi.gdata.fun/v1/chat/completions
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://dmwapi.gdata.fun/v1/chat/completions"))
                .header("Contend-Type","application/json")
                .POST(HttpRequest.BodyPublishers.ofString("name==crazyit.org&pass=leegang"))
                .build();
        String body = client.send(request, HttpResponse.BodyHandlers.ofString()).body();
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(body);
        System.out.println(node.get("error").get("message"));
    }

    public static void main5(String[] args) {

    }

}